import { InjectionKey } from "vue";
import { createStore, useStore as baseUseStore, Store } from "vuex";
import autoImport from "./util/autoimport";

export interface State {
  count: number;
}

export const key: InjectionKey<Store<State>> = Symbol();

const modules = {} as any;

const context = require.context("./modules", true, /store.ts/);

context.keys().map((key) => {
  modules[key.replace(/(\.\/|\/store|\.ts)/g, "")] =
    context(key).default || context(key);
});

Object.keys(modules).forEach((item) => {
  modules[item]["namespaced"] = true;
});

console.log(modules);

export const store = createStore<State>({
  modules,
});

// define your own `useStore` composition function
export function useStore() {
  return baseUseStore(key);
}
